<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="项目编号" prop="vProjectNo">
        <el-input
          v-model="queryParams.vProjectNo"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请购单编号" prop="vBuyNo">
        <el-input
          v-model="queryParams.vBuyNo"
          placeholder="请输入请购单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零件名称" prop="vSpaName">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入零件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="vSpaModel">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="vSpaBrand">
        <el-input
          v-model="queryParams.vSpaBrand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="请购单编号" align="center" prop="vBuyNo" />
      <el-table-column label="项目编号" align="center" prop="vProjectNo" />
      <el-table-column label="项目描述" align="center" prop="vProjectDec" />
      <el-table-column label="零件名称" align="center" prop="vSpaName" />
      <el-table-column label="规格型号" align="center" prop="vSpaModel" />
      <el-table-column label="品牌" align="center" prop="vSpaBrand" />
      <el-table-column label="单位" align="center" prop="vUnit" />

      <el-table-column label="数量" align="center" prop="nQty" />
      <el-table-column
        label="一般库存在库数量"
        align="center"
        prop="nYibanInvQty"
      />
      <el-table-column label="项目已有数量" align="center" prop="nInvQty" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.buy_requestitem_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="invTransform(scope.row)"
            >库存划转</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleToPurchase(scope.row)"
            >处理完成</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { list, invTransform, handleToPurchase } from "@/api/inv/inv0015";

export default {
  name: "inv0015",
  dicts: ["buy_requestitem_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单明细表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vSpaName: null,
        vSpaModel: null,
        vSpaBrand: null,
        vUnit: null,
        nQty: null,
        nPrice: null,
        status: null,
        nDays: null,
        vProjectNo: null,
        vProjectDec: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      selectionItems: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请购单明细列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams);
      list(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nMainId: null,
        vSpaName: null,
        vSpaModel: null,
        vSpaBrand: null,
        vUnit: null,
        nQty: null,
        nPrice: null,
        status: null,
        nDays: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        vProjectNo: null,
        vProjectDec: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selectionItems = selection;
    },

    // 转库存
    invTransform(row) {
      invTransform(row).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },

    handleToPurchase(row) {
      const id = row.id || this.ids;
      handleToPurchase(id).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
  },
};
</script>
